app.controller("DictCtl", ['$scope','$rootScope','$http','$state','$stateParams','$timeout',function($scope, $rootScope, $http, $state,$stateParams,$timeout) {
	
	
	$scope.selected = null;

	function selectTreeData(rootNode,id){
		if(!id||id.length!=32){
			return rootNode;
		}else{
			if(rootNode.children){
				for(var i=0;i<rootNode.children.length;i++){
					if(rootNode.children[i].data.id == id){
						return rootNode.children[i];
					}else{
						var result = selectTreeData(rootNode.children[i],id)
						if(result){
							return result;
						}
					}
				}
			}
			
		}
	}
	
	function getTreeData() {
		$http.get($scope.app.basePath+ 'sys/dict_group/find_all_tree.json').then(function(response) {
			if (!response.data.success) {
				$scope.authError = response.data.errorMessage;
			} else {
				$scope.treeData = response.data.data;
				$timeout(function(){
					if($stateParams.id){
						var a = selectTreeData($scope.treeData[0],$stateParams.id)
						$scope.tree.select_branch(a)
						
					}else{
						$scope.tree.select_branch($scope.treeData[0])
					}
				},0);
				
				
			}
		}, function(x) {
			$scope.authError = 'Server Error';
		});
	}
	
	
	$scope.init = function() {
		$scope.tree = {};
		getTreeData();
	}
	$scope.selectedClick = function(branch) {
		if(branch.data){
			$scope.selected = branch.data.id;
		}else{
			$scope.selected = "null";
		}
		if($state.current.name == 'app.dict.newgroup'){
			$state.go('app.dict.newgroup',{id:$scope.selected});
		}else if($state.current.name == 'app.dict.list'){
			$state.go('app.dict.list',{id:$scope.selected});
		}
		
	};
	
	$scope.$on('dict.add', function(event, data) {  
         // $scope.tree.get_selected_branch();
         var a = selectTreeData($scope.treeData[0],data.pid);
         var b = $scope.tree.select_branch(a)
        return $scope.tree.add_branch(b, {
          label: data.name,
          data: data
        });
    });  
  
	
}]);


app.controller("DictNewGroupCtl",['$scope','$http','$state','$stateParams',function($scope,$http,$state,$stateParams){
	$scope.init =function(){
		$scope.parentId = $stateParams.id;
		$scope.dictGroup = {};
		if(!$scope.parentId){
			$state.go('app.dict.list');
		}else if($scope.parentId=='null'){
			$scope.parentId = null;
		}
	}
	
	$scope.submit=function(){
		$http.post($scope.app.basePath+ 'sys/dict_group/save.json',{name:$scope.dictGroup.name,code:$scope.dictGroup.code,depict:$scope.dictGroup.depict,pid:$scope.parentId}).then(function(response) {
			if (!response.data.success) {
				$scope.authError = response.data.errorMessage;
			} else {
				$scope.$emit('dict.add', response.data.data); 
			}
		}, function(x) {
			$scope.authError = 'Server Error';
		});
	}
	
}]);







app.controller("DictListCtl", ['$scope','$rootScope','$http','$state','$stateParams',function($scope, $rootScope, $http, $state,$stateParams) {
	
	function getData() {
		var url = '';
		if($scope.grouptId){
			url = $scope.app.basePath+ 'sys/dict/find_group.json?groupId='+$scope.grouptId;
		}else{
			url = $scope.app.basePath+ 'sys/dict/find_all.json';
		}
		
		$http.get(url).then(function(response) {
			if (!response.data.success) {
				$scope.authError = response.data.errorMessage;
			} else {
				$scope.dicts = response.data.data;
			}
		}, function(x) {
			$scope.authError = 'Server Error';
		});
	}

	
	
	$scope.init = function() {
		$scope.grouptId = $stateParams.id;
		if(!$scope.grouptId||$scope.grouptId=='null'){
			$scope.grouptId = null;
		}
		getData();
		$scope.dict = {};
	}
	
	$scope.add = function(){
		$scope.dict.groupId = $scope.grouptId;
		console.log($scope.dict)
		$http.post($scope.app.basePath+ 'sys/dict/save.json',$scope.dict).then(function(response) {
			if (!response.data.success) {
				$scope.authError = response.data.errorMessage;
			} else {
				$scope.dict = {};
				getData();
			}
		}, function(x) {
			$scope.authError = 'Server Error';
		})
	}
	
	$scope.removeRow=function(id){
		$http.get($scope.app.basePath+ 'sys/dict/delete_id.json?id='+id).then(function(response) {
			if (!response.data.success) {
				$scope.authError = response.data.errorMessage;
			} else {
				getData();
			}
		}, function(x) {
			$scope.authError = 'Server Error';
		});
	}
	
  
	
}]);